package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import com.project.comom.CommonFileService;
import com.project.domain.bo.AllAlterationInfoBo;
import com.project.domain.bo.AlterationBo;
import com.project.domain.entity.BizProjectAlteration;
import com.project.service.IBizProjectAlterationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 项目变更
 */

@Api(tags = "项目变更")
@RestController
@RequestMapping("/pro/alteration")
public class AlterationController extends BaseController {

    @Autowired
    private IBizProjectAlterationService bizProjectAlterationService;

    @Autowired
    CommonFileService commonFileService;

    @ApiOperation("列表左上方统计")
    @GetMapping("/groups")
    public AjaxResult groups(AlterationBo alterationBo){
        return success(bizProjectAlterationService.selectGroups(alterationBo));
    }

    /**
     * 查询项目变更列表
     */
    @ApiOperation("查询项目变更列表")
    @GetMapping("/list")
    public TableDataInfo list(AlterationBo alterationBo)
    {
        IPage<BizProjectAlteration> proList = bizProjectAlterationService.selectBizProjectAlterationList(alterationBo,"select");
        return TableDataInfo.build(proList.getRecords(),proList.getTotal());
    }

    /**
     * 导出项目变更列表
     */
    @ApiOperation("导出项目变更列表")
    @Log(title = "项目变更", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody AlterationBo alterationBo)
    {
        IPage<BizProjectAlteration> list = bizProjectAlterationService.selectBizProjectAlterationList(alterationBo,"export");
        ExcelUtil<BizProjectAlteration> util = new ExcelUtil<>(BizProjectAlteration.class);
        util.showColumn(alterationBo.getExportStr().split(","));
        util.exportExcelByOrder(response,list.getRecords(),"项目变更数据",alterationBo.getExportStr().split(","));
    }

    /**
     * 获取项目变更详细信息
     */
    @ApiOperation("获取项目变更详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(bizProjectAlterationService.selectBizProjectAlterationByID(ID));
    }

    /**
     * 保存项目变更
     */
    @ApiOperation("新增项目变更")
    @Log(title = "项目变更", businessType = BusinessType.INSERT)
    @PostMapping("/saveAlter")
    public AjaxResult saveAlter(@RequestBody AllAlterationInfoBo bizProjectAlterationBo)
    {
        return bizProjectAlterationService.saveBizProjectAlteration(bizProjectAlterationBo);
    }

    /**
     * 删除项目变更
     */
    @ApiOperation("删除")
    @Log(title = "项目变更", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        String res = bizProjectAlterationService.deleteBizProjectAlterationByIDs(IDs);
        if(res.contains("成功")){
            return success(res);
        }else{
            return error(res);
        }
    }
}
